package com.relife.common.model;

import java.util.ArrayList;
import java.util.List;

import com.relife.common.utils.StringUtil;
import org.hibernate.criterion.Order;



@SuppressWarnings("rawtypes")
public class Page {
	/** 当前页 **/
	private int page = 1;
	private List list;
	/** 每页条数 **/
	private int rows;
	/** 总记录数 **/
	private int total;
	/** 排序字段名称 **/
	private String sort;
	/** 排序类型 **/
	private String order;

	private String fields;
	/** 总页数 **/
	private int pages;

	private boolean excel = false;

	private String tagId;

	/** 树形列表时候使用对应对象的子集合名称 **/
	private String children = "children";

	public Page() {
	}

	public Page(int page, int total) {
		this.total = total;
		this.page = page;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public List getList() {
		if (list == null) {
			list = new ArrayList<Object>();
		}
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public String getChildren() {
		return children;
	}

	public void setChildren(String children) {
		this.children = children;
	}

	public String[] getField() {
		return StringUtil.isEmpty(this.fields) ? null : fields.split(",");
	}

	public Order getOrderBy() {
		if (StringUtil.isNotEmpty(sort)) {
			if ("asc".equals(this.order)) {
				return Order.asc(this.sort);
			} else {
				return Order.desc(this.sort);
			}
		} else {
			return null;
		}
	}

	public String getFields() {
		return fields;
	}

	public void setFields(String fields) {
		this.fields = fields;
	}

	public int getPages() {
		if (rows >= total) {
			pages = 1;
		} else {
			int m = this.total % this.rows;
			pages = (total - m) / rows + (m > 0 ? 1 : 0);
		}
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public boolean isExcel() {
		return excel;
	}

	public void setExcel(boolean excel) {
		this.excel = excel;
	}

	public String getTagId() {
		return tagId;
	}

	public void setTagId(String tagId) {
		this.tagId = tagId;
	}

	public int getStart() {
		return (page - 1) * rows;
	}

}
